package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.LogBean;
import com.project.bean.SellBean;
import com.project.bean.SellOrderBean;
import com.project.bean.UserBean;
import com.project.service.ILogService;
import com.project.service.ISellOrderService;
import com.project.service.ISellService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("sellCar")
@Api(tags = "销售车控制层")
public class SellCarController {

    @Autowired
    private ISellService sellCarService;
    @Autowired
    private ISellOrderService sellOrderService;


    @ApiOperation("展示某个用户的销售车")
    @GetMapping("findAll")
    public List<SellBean> findAll(HttpServletRequest request) {
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        return sellCarService.findAll(loginUser.getId());
    }

    @PostMapping("add")
    @ApiOperation("添加销售车")
    public String add(String goodsId, HttpServletRequest request) {
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        sellCarService.add(loginUser.getId(), goodsId);
        return "ok";
    }

    @DeleteMapping("delete")
    @ApiOperation("删除销售车")
    public String delete(Integer sellCarId) {
        sellCarService.deleteBySellId(sellCarId);
        return "ok";
    }

    //出库
    @PostMapping("outGoods")
    @ApiOperation("出库")
    public String outGoods(String customName, String address, Integer total, HttpServletRequest request) {
        //登录用户
        UserBean loginUser = (UserBean) request.getSession().getAttribute("loginUser");
        //添加订单
        sellOrderService.add(new SellOrderBean(total,address,customName, loginUser.getRealName()),loginUser.getId());
        //删除销售车
        sellCarService.deleteByUserId(loginUser.getId());

        return "ok";
    }

    @PutMapping("updateSellNum")
    @ApiOperation("修改销售数量")
    public String updateSellNum(Integer sellCarId,Integer newSellNum){
        sellCarService.updateSellNum(sellCarId,newSellNum);
        return "ok";
    }


}